<?php
	
	include ('chequearUsuarioLogueado.php');
	include ('conectarMarcas.php');	
		
	$sqlEntrada="SELECT distinct em.IDEmpleado, em.emNombre, em.emApellido, mar.marFecha, min(mar.marHora), mar.marTipoMarca FROM empleados AS em inner join marcas as mar on em.idEmpleado = mar.marIDEmpleado WHERE mar.marTipoMarca = 'E' GROUP BY mar.marIDempleado, mar.marFecha;";	
	$sqlSalida="SELECT distinct em.IDEmpleado, em.emNombre, em.emApellido, mar.marFecha, max(mar.marHora), mar.marTipoMarca FROM empleados AS em inner join marcas as mar on em.idEmpleado = mar.marIDEmpleado WHERE mar.marTipoMarca = 'S' GROUP BY mar.marIDempleado, mar.marFecha;";
	
	$resultEntrada=mysql_query($sqlEntrada);
	$resultSalida=mysql_query($sqlSalida);

	echo "<table border='0' cellpadding='1' cellspacing='3'>
			<tr bgcolor='#0057A6' style= 'color :#FFFFFF;' class='titulo'>
				<th>Empleado</th>
				<th>Fecha</th>								
				<th>Entrada</th>
				<th>Salida</th>
			</tr>";
	
	while($row=mysql_fetch_assoc($resultEntrada))
	{
		$arrayEntrada[] = $row;
	}
	while($row=mysql_fetch_assoc($resultSalida))
	{
		$arraySalida[] = $row;		
	}
	
	for ($i = 0; $i < count($arraySalida); $i++)
	{	
		 if (!in_multi_array($arraySalida[$i]['marFecha'], $arrayEntrada))
		{			
			$arrayAux['IDEmpleado'] = $arraySalida[$i]['IDEmpleado'];
			$arrayAux['emNombre'] = $arraySalida[$i]['emNombre'];
			$arrayAux['emApellido'] = $arraySalida[$i]['emApellido'];
			$arrayAux['marFecha'] = $arraySalida[$i]['marFecha'] ;
			$arrayAux['min(mar.marHora)'] = "";
			$arrayAux['marTipoMarca'] = "E";
			
			array_push($arrayEntrada, $arrayAux);			
			array_sort_by_column($arrayEntrada, 'IDEmpleado');
		}
	}
	
	for ($i = 0; $i < count($arrayEntrada); $i++)	
	{
		for($j = 0; $j < count($arraySalida); $j++)
		{
			if (($arraySalida[$j]['marFecha'] == $arrayEntrada[$i]['marFecha']) && ($arraySalida[$j]['IDEmpleado'] == $arrayEntrada[$i]['IDEmpleado']))
			{
				$newArray[$i]['Nombre'] = $arrayEntrada[$i]['emNombre'];
				$newArray[$i]['Apellido'] = $arrayEntrada[$i]['emApellido'];
				$newArray[$i]['Fecha'] = $arrayEntrada[$i]['marFecha'];
				$newArray[$i]['Entrada'] = $arrayEntrada[$i]['min(mar.marHora)'];
				$newArray[$i]['Salida'] = $arraySalida[$j]['max(mar.marHora)'];
				break;				
			}
		}
	}
	
	for ($i = 0; $i < count($newArray); $i++)
	{
		$fecha = date ('d/m/y', strtotime($newArray[$i]['Fecha']));
		
		echo "<tr align='center' class='subtitulo'>";
		echo "<td>" . $newArray[$i]['Nombre'] . " " . $newArray[$i]['Apellido'] . "</td>";
		echo "<td>" . $fecha . "</td>";
		echo "<td>" . $newArray[$i]['Entrada']. "</td>";
		echo "<td>" . $newArray[$i]['Salida']. "</td>";
		echo "</tr>";
	}
	echo "</table>";
	

	function in_multi_array($needle, $haystack)
	{
		foreach($haystack as $pos => $val)
		{
			if (is_array($val))
			{
				if (in_multi_array($needle, $val))
				return 1;
			}
			else
			if ($val == $needle)
				return 1;
		}
	}
	
	function array_sort_by_column(&$arr, $col, $dir = SORT_ASC) {
		$sort_col = array();
		foreach ($arr as $key=> $row) {
			$sort_col[$key] = $row[$col];
		}

		array_multisort($sort_col, $dir, $arr);
	}


?>